﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

using DotNetNuke.Entities.Modules;
using DesertedRoadStudios.BlogNetNuke.Common;
using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke.UI.Widgets
{
    public class TagCloud : PortalModuleBase
    {
        public List<BlogTag> BlogTags { get { return _tags; } }

        private List<BlogTag> _tags;
        private XDocument _settings;
        private Dictionary<string, string> _widgetSettings;
        private int _tagsMaximum;

        public class BlogTag : DesertedRoadStudios.BlogNetNuke.Entities.Tag.TagInfo
        {
            public int Weight = 0;
        }

        public TagCloud()
        {
        }

        public TagCloud(XDocument settings, Dictionary<string, string> widgetSettings, IEnumerable<PostInfo> posts)
        {
            _settings = settings;
            _widgetSettings = widgetSettings;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            CheckWidget();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RenderTags();
        }

        private void CheckWidget()
        {
            if (!_widgetSettings["Widget"].Equals("TagCloud"))
                throw new System.Exception("Control doesn't implement DesertedRoadStudios.BlogNetNuke.UI.Widgets." + _widgetSettings["Widget"]);

            if (!_widgetSettings.ContainsKey("TagsMaximum"))
                _tagsMaximum = 20;
            else
                _tagsMaximum = Convert.ToInt32(_widgetSettings["TagsMaximum"]);
        }

        private void RenderTags()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            IEnumerable<PostsTags> tags = bnn.GetAllTagsForPosts(Convert.ToInt32(_settings.Element("params").Element("Blog").Element("BlogDetails").Element("PortalID").Value), Convert.ToInt32(_settings.Element("params").Element("Blog").Element("BlogDetails").Element("ModuleID").Value));
            
            List<BlogTag> tempTags = new List<BlogTag>();

            foreach (int tagID in tags.Select(t => t.TagID).Distinct())
            {
                BlogTag tempTag = new BlogTag();
                PostsTags postsTag = tags.Where(t => t.TagID == tagID).First();
                tempTag.Tag = postsTag.Tag;
                tempTag.TagID = postsTag.TagID;
                tempTag.ModuleID = postsTag.ModuleID;
                tempTag.PortalID = postsTag.PortalID;
                tempTag.Slug = postsTag.Slug;
                tempTag.TagLink = Methods.GetPathExtension(_settings, _settings.Element("params").Element("Blog").Element("BlogDetails").Element("TabPath").Value.Replace("//", "/") + "/Tag/" + postsTag.Slug);
                tempTag.Weight = tags.Where(t => t.TagID == tagID).Count();

                tempTags.Add(tempTag);
            }
            
            _tags = tempTags.OrderByDescending(t => t.Weight).Take(_tagsMaximum).ToList<BlogTag>();
            decimal totalWeight = _tags.Sum(t => t.Weight);

            foreach (BlogTag tag in _tags)
                tag.Weight = Convert.ToInt32((Convert.ToDecimal(tag.Weight) / totalWeight) * 10);

            _tags = _tags.Randomize().ToList<BlogTag>();
        }
    }
}